package com.issac.mmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.issac.mmall.entity.*;
import com.issac.mmall.mapper.*;
import com.issac.mmall.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.issac.mmall.service.ProductService;
import com.issac.mmall.vo.OrderProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Issac
 * @since 2020-12-10
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ProductMapper productMapper;
    @Override
    public boolean save(Orders orders, User user,String address, String remark) {
        //新增用户地址
        if (orders.getUserAddress().equals("newAddress")){
            UserAddress userAddress = new UserAddress();
            userAddress.setUserId(user.getId());
            userAddress.setRemark(remark);
            userAddress.setAddress(address);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("isdefault",1);
            queryWrapper.eq("user_id",user.getId());
            UserAddress userAddress1 = userAddressMapper.selectOne(queryWrapper);
            if (userAddress1!=null){
                userAddress1.setIsdefault(0);
                userAddressMapper.updateById(userAddress1);
            }
            userAddress.setIsdefault(1);
            userAddressMapper.insert(userAddress);
            orders.setUserAddress(address);
        }
        orders.setUserId(user.getId());
        orders.setLoginName(user.getLoginName());
        String seriaNumber = null;
        try {
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < 32; i++) {
                stringBuffer.append(Integer.toHexString(new Random().nextInt(16)));
            }
            seriaNumber = stringBuffer.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        orders.setSerialnumber(seriaNumber);

        //执行完之后，Mybatis Plus会将orders回填，因此可以拿到最新的orders记录
        ordersMapper.insert(orders);

        //结算购物车
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getId());
        List<Cart> cartList = cartMapper.selectList(queryWrapper);
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart,orderDetail);
            orderDetail.setOrderId(orders.getId());
            //BeanUtils会将cart的主键id赋给order_detail表，重赋null让mybatisPlus控制自增
            orderDetail.setId(null);
            orderDetailMapper.insert(orderDetail);
        }
        //清空购物车
        cartMapper.delete(queryWrapper);

        return true;
    }

    @Override
    public List<OrderProductVO> findAllOrderProductVOByUserId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<OrderProductVO> orderProductParentVOList = new ArrayList<>();
        queryWrapper.eq("user_id",id);
        //拿到订单
        List<Orders> ordersList = ordersMapper.selectList(queryWrapper);
        for (Orders orders : ordersList) {
            List<OrderProductVO> orderProductChildrenVOList = new ArrayList<>();
            OrderProductVO parentVO = new OrderProductVO();
            queryWrapper = new QueryWrapper();
            queryWrapper.eq("order_id",orders.getId());
            //拿到订单详情表
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(queryWrapper);
            for (OrderDetail orderDetail : orderDetailList) {
                OrderProductVO childrenVO = new OrderProductVO();
                //根据订单详情，查询到商品信息
                Product product = productMapper.selectById(orderDetail.getProductId());
                //把商品信息放到childrenVO
                childrenVO.setProductList(product);
                //把订单详情放到childrenVO
                BeanUtils.copyProperties(orderDetail,childrenVO);
                //将每一条childrenVO放到list里面
                orderProductChildrenVOList.add(childrenVO);

            }
            //把一条订单表的数据放进parentVO里
            BeanUtils.copyProperties(orders,parentVO);
            parentVO.setChildren(orderProductChildrenVOList);
            orderProductParentVOList.add(parentVO);
        }
        return orderProductParentVOList;
    }
}
